package com.mars.common.utils;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.AnonymousCOSCredentials;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.UUID;

/**
 * 腾讯云对象云存储工具类
 */
public class TenXCOS {
    private final static String secretId = "AKIDzoMefPs52j9IAl6AxUkjRs2vXsvhDSQ7";
    private final static String secretKey = "SdXBXcQwB9qGPHsCG4RXMuBBuNZT7TYL";
    /**
     * 区域名称
     */
    private final static String TX_AREA = "ap-beijing";

    /**
     * 文件上传 腾讯云
     *
     * @param multipartFile
     * @param key
     * @param bucketName
     * @return
     */
    public static String upload(MultipartFile multipartFile, String key, String bucketName) {
        //文件上传前的名称
        String fileName = UUID.randomUUID() + multipartFile.getOriginalFilename();
        File localFile = new File(fileName);
        OutputStream out = null;
        try {
            out = new FileOutputStream(localFile);
            byte[] ss = multipartFile.getBytes();
            for (int i = 0; i < ss.length; i++) {
                out.write(ss[i]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        Region region = new Region(TX_AREA);
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        key = key + fileName;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        cosClient.putObject(putObjectRequest);
        URL url = cosClient.getObjectUrl(bucketName, key);
        File f = new File(localFile.toURI());

        if (f.delete()) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        return String.valueOf(url);
    }

    /**
     * File格式文件直接上传
     *
     * @param file
     * @param key
     * @param bucketName
     * @return
     */
    public static String upload(File file, String key, String bucketName) {

        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        Region region = new Region(TX_AREA);
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        key = key + file.getName();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        cosClient.putObject(putObjectRequest);
        URL url = cosClient.getObjectUrl(bucketName, key);
        return String.valueOf(url);
    }

    /**
     * 文件删除
     *
     * @param key
     * @param bucketName
     */
    public static void deletedFile(String key, String bucketName) {
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region(TX_AREA);
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        cosClient.deleteObject(bucketName, key);
    }

    /**
     * 获取文件路径
     *
     * @param key
     * @param bucketName
     * @return
     */
    public static URL getUrl(String key, String bucketName) {
        COSCredentials cred = new AnonymousCOSCredentials();
        ClientConfig clientConfig = new ClientConfig(new Region(TX_AREA));
        // 设置生成的 url 的协议名
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);

        URL url = cosclient.getObjectUrl(bucketName, key);
        return url;
    }

    /**
     * 将MultipartFile 转成 File格式
     *
     * @param multipartFile
     * @return
     */
    public static File toFile(MultipartFile multipartFile) {
        String fileName = UUID.randomUUID() + multipartFile.getOriginalFilename();
        File localFile = new File(fileName);
        OutputStream out = null;
        try {
            out = new FileOutputStream(localFile);
            byte[] ss = multipartFile.getBytes();
            for (int i = 0; i < ss.length; i++) {
                out.write(ss[i]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return localFile;
    }


}
